package com.whyx.project.openapi.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
//import com.whyx.common.utils.DateUtil;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class JWTUtils {
    //jwt签名key
    private static String key = "7A9EF8C4DF7FB166349E7D936DD5604D";
    public static final String WX_PRO_TYPE = "wxpro";
    public static final String APP_TYPE = "app";

    /**
     * 创建token
     * @return
     * @throws Exception
     */
    public static String createToken(Map<String, String> tokenMap) throws JWTDecodeException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");
        String token = JWT.create()
                .withHeader(map)//header
                .withClaim("userUuid", tokenMap.get("userUuid"))
                .withClaim("timestamp", tokenMap.get("timestamp"))
                .withClaim("type", tokenMap.get("type"))
                .sign(Algorithm.HMAC256(key));//加密
        return token;
    }

    /**
     * 解密
     */
    public static HashMap<String, String> decryptToken(String token) throws JWTDecodeException {
        HashMap<String, String> map = new HashMap<>();
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(key)).build();
        DecodedJWT jwt = verifier.verify(token);
        Map<String, Claim> claims = jwt.getClaims();
        String userUuid = claims.get("userUuid").asString();
        String timestamp = claims.get("timestamp").asString();
        String type = claims.get("type").asString();

        map.put("userUuid", userUuid);
        map.put("timestamp", timestamp);
        map.put("type", type);
        return map;
    }

    public static String getUserUuid(String token) {
        try {
            if (!StringCls.isEmpty(token)) {
                //解密
                HashMap<String, String> map = JWTUtils.decryptToken(token);
                String userUuid = map.get("userUuid");
                if (!StringCls.isEmpty(userUuid)) {
                    return userUuid;
                }
            }
            return "";
        } catch (Throwable e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 验证token
     * @throws Exception
     */
    public static Boolean verifyToken(HashMap<String, String> map) {
        Long timestamp = null;
        if (!StringCls.isEmpty(map.get("timestamp"))) {
            timestamp = Long.valueOf(map.get("timestamp"));
        }
        Long nowTime = new Date().getTime();
        if (timestamp != null && nowTime != null) {
            Long diffMS = (timestamp - nowTime);
            if (!StringCls.isEmpty(map.get("userUuid")) && diffMS > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取消息头
     */
    public static String getMsgHeader(HttpServletRequest request, String msgHeader) {
        if ((!"".equals(msgHeader) && msgHeader != null) && request != null) {
            String header = request.getHeader(msgHeader);
            if (header != null && !"".equals(header)) {
                return header;
            }
        }
        return null;
    }

    public static String getToken(String userUuid, String timestamp, String type) throws JWTDecodeException {
        //生成token
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("userUuid", userUuid);
        tokenMap.put("timestamp", timestamp);
        tokenMap.put("type", type);

        return createToken(tokenMap);
    }

}
